package com.jinghao.ctws.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.jinghao.core.common.exception.JingHaoCustomerException;
import com.jinghao.core.common.service.impl.JingHaoBaseServiceImpl;
import com.jinghao.core.common.util.cnd.JhCnd;
import com.jinghao.ctws.entity.CwvFlow;
import com.jinghao.ctws.entity.CwvFlow.ECwvFlow;
import com.jinghao.ctws.entity.CwvProcessConfig;
import com.jinghao.ctws.service.ICwvFlowService;
import com.jinghao.ctws.service.ICwvProcessConfigService;
import com.jinghao.ctws.util.EJhCTwsCode;

@Service("cwvFlowService")
public class CwvFlowServiceImpl extends JingHaoBaseServiceImpl<CwvFlow> implements ICwvFlowService {
	//==================定制内容开始======================
	
//在这里修改代码
	
	@Resource
	private ICwvProcessConfigService cwvProcessConfigService;
	
    /**
	 * 流程列表（包含模糊查询）
	 */
	@Override
	public void listCwvFlow(CwvFlow model) {
		if (StringUtils.isEmpty(model.getFjhorgUuid())) {
			throw new JingHaoCustomerException(EJhCTwsCode.CTWS2001.getCode(),"仓库UUID不能为空，请重新提供仓库UUID！"); 
		}
		JhCnd cnd=new JhCnd();
		cnd.andEquals("fjhorgUuid");
		cnd.andNotEquals("status");
		model.setStatus(ECwvFlow.status_2.getKey());
		if(model.getName() != null && !model.getName().trim().equals("")){
			cnd.andLike("name");
			model.setName("%"+model.getName()+"%");
		}
		model.lnkCnd(cnd);
		model.setMapList(this.queryMapListById(model, "listCwvFlowALL"));
	}

	@Override
	public void getSingleCwvFlowInfo(CwvFlow model) {
		if(model.getFlowUuid()==null||StringUtils.isEmpty(model.getFlowUuid().toString())){
			throw new JingHaoCustomerException(EJhCTwsCode.TWS3001.getCode(),"请提供流程UUID信息！");
		}
		JhCnd cnd=new JhCnd();
		cnd.andEquals("flowUuid");
		model.lnkCnd(cnd);
		List<Map<String,Object>> list=this.queryMapList(model);
		if(list==null||list.size()<=0||list.size()>1){
			throw new JingHaoCustomerException(EJhCTwsCode.TWS3001.getCode(),"提供流程UUID信息有误,查询不到相关信息！");
		}
		Map<String,Object> objMap=list.get(0);
		if(objMap.get("code")==null){
			throw new JingHaoCustomerException(EJhCTwsCode.TWS3001.getCode(),"提供流程编号信息有误,查询不到相关信息！");
		}
		CwvProcessConfig wpModel=new CwvProcessConfig();
		wpModel.setFflowUuid(model.getFlowUuid());
		String code=objMap.get("code").toString();
		cnd.reset();
		cnd.andEquals("fflowUuid");
		cnd.orderBy("w.sort", "asc");
		wpModel.setFcode(code);
		wpModel.lnkCnd(cnd);
		wpModel.setPage(null);
		List<Map<String,Object>> wvplist=this.cwvProcessConfigService.queryMapListById(wpModel, "queryMapListCwvProcessconfigByFcode");
		objMap.put("processInfoList", this.getCwvProcessConfigList(wvplist));
		model.setMapList(list);	
	}
	  /**
     * 获取环节数据
     * @param wvplist
     * @return
     */
	public List<Map<String,Object>> getCwvProcessConfigList(List<Map<String,Object>> wvplist){
		List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		Set<String> dateSet=new HashSet<String>();
		String processId="";
		Map<String,Object> processMap=null;
		//第一步，获取环节数据
		for(Map<String,Object> map:wvplist){
			if(map.get("processId")==null){
				continue;
			}
			processId=map.get("processId").toString();
			if(!dateSet.contains(processId)){
				processMap=new HashMap<String,Object>();
				processMap.put("processId", processId);
				processMap.put("name", map.get("name")!=null?map.get("name").toString():"");
				dateSet.add(processId);
				//第二步，获取位置编码信息
				processMap.put("locationIdList", this.getCwvProcessLocationIdList(wvplist,processId));
				list.add(processMap);
			}
		}
		dateSet.clear();
		return list;
		
	}
	/**
	 * 获取位置编码信息
	 * @param wvplist
	 * @param processId
	 * @return
	 */
	public List<Map<String,Object>> getCwvProcessLocationIdList(List<Map<String,Object>> wvplist,String processId){
		List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		Set<String> dateSet=new HashSet<String>();
		String locationId="";
		Map<String,Object> locationMap=null;
		for(Map<String,Object> map:wvplist){
			if(map.get("locationId")==null){
				continue;
			}
			locationId=map.get("locationId").toString();
			if(!dateSet.contains(locationId)&&map.get("processId")!=null&&map.get("processId").toString().equals(processId)){
				locationMap=new HashMap<String,Object>();
				locationMap.put("locationId", locationId);
				locationMap.put("fcameraUuidList", this.getWvProcessdeviceIdList(wvplist, locationId,processId));
				dateSet.add(locationId);
				list.add(locationMap);
			}
		}
		return list;
	}
	/**
	 * 获取环节位置编码对应设备信息
	 * @param wvplist
	 * @param processId
	 * @return
	 */
	public List<Map<String,Object>> getWvProcessdeviceIdList(List<Map<String,Object>> wvplist,String locationId,String processId){
		List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		Set<String> dateSet=new HashSet<String>();
		String fcameraUuid="";
		Map<String,Object> fdeviceMap=null;
		for(Map<String,Object> map:wvplist){
			if(map.get("cameraUuid")==null){ 
				continue;
			}
			fcameraUuid=map.get("cameraUuid").toString();
			//if(map.get("status")!=null&&map.get("status").toString().equals(String.valueOf(EWvCamera.status_1))&&!dateSet.contains(locationId)&&map.get("locationId")!=null&&map.get("locationId").toString().equals(locationId)){
			if(!dateSet.contains(locationId)&&map.get("locationId")!=null&&map.get("locationId").toString().equals(locationId)&&map.get("processId").toString().equals(processId)){
				fdeviceMap=new HashMap<String,Object>();
				fdeviceMap.put("cameraUuid", fcameraUuid);
				fdeviceMap.put("deviceName", map.get("deviceName")!=null?map.get("deviceName").toString():"");
				list.add(fdeviceMap);
				dateSet.add(fcameraUuid);
			}
		}
		return list;
	}
	
	//==================定制内容结束======================
}
